package com.example.zengpengcheng0526;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText name;
    private EditText pw;
    private CheckBox jz;
    private TextView wj;
    private TextView zc;
    private Button login;
    private User user;
    GuangBo guangBo;
    IntentFilter filter;
    MyService.MyBinder myBinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }
    public void init()  {
        name = findViewById(R.id.name);
        pw = findViewById(R.id.pw);
        jz = findViewById(R.id.jz);
        wj = findViewById(R.id.wj);
        zc = findViewById(R.id.zc);
        login = findViewById(R.id.login);
        guangBo=new GuangBo();
        filter=new IntentFilter();
        filter.addAction("com.example.zengpengcheng0526.LoginActivity");
        registerReceiver(guangBo,filter);
        wj.setOnClickListener(this);
        zc.setOnClickListener(this);
        login.setOnClickListener(this);
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        String uname = sp.getString("name","");
        name.setText(uname);
        try {
            FileInputStream in = this.openFileInput("user.txt");
            InputStreamReader read=new InputStreamReader(in);
            BufferedReader bin=new BufferedReader(read);
            String s = bin.readLine();
            pw.setText(s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        this.user =new User();
        this.user.setUsername("123456");
        this.user.setPw("123456");
        this.user.setSex("男");
        this.user.setHobby("打游戏");
        this.user.setPhone("12345678998");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==111){
            user= (User) data.getSerializableExtra("myuser");
            name.setText(user.getUsername());
            pw.setText(user.getPw());
        }
    }
    String uname;
    String upw;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.wj:
                break;
            case R.id.zc:
                Intent intent=new Intent(LoginActivity.this,RegistActivity.class);
                startActivityForResult(intent,111);
                break;
            case R.id.login:
                 uname=name.getText().toString();
                 upw=pw.getText().toString();

               //bindService(intentt,new MyConn(),BIND_ABOVE_CLIENT);
               Intent intentt=new Intent(this,MyService.class);
               intentt.putExtra("uname",uname);
               intentt.putExtra("upw",upw);
               intentt.putExtra("user",user);
                if (jz.isChecked()){
                    startService(intentt);
                }else {
                    Toast.makeText(LoginActivity.this, "请勾选记住密码", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(guangBo);
    }

    public class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d( "onServiceConnected: ","------------------");
            myBinder= (MyService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

}
